﻿#define DEBUG_AGENT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;

namespace test2
{
    public partial class MainPage : PhoneApplicationPage
    {
        PeriodicTask periodicTask;
        ResourceIntensiveTask resourceIntensiveTask;
        //Pour les alerte
        string periodicTaskName = "PeriodicAgent";
        string resourceIntensiveTaskName = "ResourceIntensiveAgent";
        public bool agentsAreEnabled = true;
        public bool ignoreCheckBoxEvents = false;
        public IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        // Pour la modification de la tile
        ShellTileSchedule SampleTileSchedule = new ShellTileSchedule();
        bool TileScheduleRunning = false;
        //Pour le code
        public List <Article> newArticle;
        public Dictionary<String, Article> myTuple;


        // Constructeur
        public MainPage()
        {
            InitializeComponent();
            Test();
            //StartResourceIntensiveAgent();
            StartPeriodicAgent();


        }

        private void StartPeriodicAgent()
        {
            // Variable for tracking enabled status of background agents for this app.
            agentsAreEnabled = true;

            // Obtain a reference to the period task, if one exists
            periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

            // If the task already exists and background agents are enabled for the
            // application, you must remove the task and then add it again to update 
            // the schedule
            if (periodicTask != null)
            {
                RemoveAgent(periodicTaskName);
            }

            periodicTask = new PeriodicTask(periodicTaskName);

            // The description is required for periodic agents. This is the string that the user
            // will see in the background services Settings page on the device.
            periodicTask.Description = "This demonstrates a periodic task.";

            // Place the call to Add in a try block in case the user has disabled agents.
            try
            {
                ScheduledActionService.Add(periodicTask);

                // If debugging is enabled, use LaunchForTest to launch the agent in one minute.
                ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(20));

            }
            catch (InvalidOperationException exception)
            {
                if (exception.Message.Contains("BNS Error: The action is disabled"))
                {
                    MessageBox.Show("Background agents for this application have been disabled by the user.");
                    agentsAreEnabled = false;
                }
            }
        }

        private void StartResourceIntensiveAgent()
        {
            // Variable for tracking enabled status of background agents for this app.
            agentsAreEnabled = true;

            resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask;

            // If the task already exists and background agents are enabled for the
            // application, you must remove the task and then add it again to update 
            // the schedule.
            if (resourceIntensiveTask != null)
            {
                RemoveAgent(resourceIntensiveTaskName);
            }

            resourceIntensiveTask = new ResourceIntensiveTask(resourceIntensiveTaskName);

            // The description is required for periodic agents. This is the string that the user
            // will see in the background services Settings page on the device.
            resourceIntensiveTask.Description = "This demonstrates a resource-intensive task.";

            // Place the call to Add in a try block in case the user has disabled agents.
            try
            {
                ScheduledActionService.Add(resourceIntensiveTask);
                ScheduledActionService.LaunchForTest(resourceIntensiveTaskName, TimeSpan.FromSeconds(20));
            }
            catch (InvalidOperationException exception)
            {
                if (exception.Message.Contains("BNS Error: The action is disabled"))
                {
                    MessageBox.Show("Background agents for this application have been disabled by the user.");
                    agentsAreEnabled = false;
                }
            }
        }

        //private void PeriodicCheckBox_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (ignoreCheckBoxEvents)
        //        return;
        //    StartPeriodicAgent();
        //}

        //private void PeriodicCheckBox_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    if (ignoreCheckBoxEvents)
        //        return;
        //    RemoveAgent(periodicTaskName);
        //}

        //private void ResourceIntensiveCheckBox_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (ignoreCheckBoxEvents)
        //        return;
        //    StartResourceIntensiveAgent();
        //}

        //private void ResourceIntensiveCheckBox_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    if (ignoreCheckBoxEvents)
        //        return;
        //    RemoveAgent(resourceIntensiveTaskName);
        //}

        private void RemoveAgent(string name)
        {
            try
            {
                ScheduledActionService.Remove(name);
            }
            catch (Exception)
            {
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

            resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask;


        }

        private void Test()
        {
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=2"));
            if (TileToFind == null)
            {
                StandardTileData NewTileData;

                NewTileData = new StandardTileData
                {

                    BackgroundImage = null,
                    Title = "Gift Idea",
                    BackTitle = "",
                    BackContent = "Gift Idea",
                    Count = 0
                };
                ShellTile.Create(new Uri("/MainPage.xaml?TileID=2", UriKind.Relative), NewTileData);
            }
        }




    }
}